package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BlackListApiImpl implements BlackListApi {
    @Autowired
    private BlackListMapper blackListMapper;
    /**
     * 我的--通用设置--黑名单分页查询
     * @param userId 登录用户id
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult findPageByUserId(Long userId, Long page, Long pagesize) {

        //构建分页对象
        IPage<BlackList> iPage = new Page<>(page,pagesize);
        //构建查询条件
        QueryWrapper<BlackList> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        //按照时间降序
        wrapper.orderByDesc("created");
        //查询
        blackListMapper.selectPage(iPage,wrapper);
        //构建分页查询
        PageResult pageResult = new PageResult();
        pageResult.setItems(iPage.getRecords());
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setCounts(iPage.getTotal());
        pageResult.setPages(iPage.getPages());

        return pageResult;
    }

    /**
     * 通用设置--移除黑名单
     * @param blackUserId 移除黑名单id
     * @param loginUserID 登录用户id
     */
    // 为什么要设置两个参数 -- 因为会删除整列数据
    @Override
    public void delete(Long blackUserId, Long loginUserID) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",loginUserID);
        wrapper.eq("black_user_id",blackUserId);
        blackListMapper.delete(wrapper);

    }
}
